﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO_HealthInsurance;
using System.Data;
using System.Data.SqlClient;

namespace DAO_HealthInsurance
{
  public  class DAONews:ConnectDatabase
  {
      public SqlCommand Cmd;
      public DTONews objNew;
      public DataTable ListNews()
      {
          try
          {
              OpenConnect();
              Cmd = new SqlCommand("listnews", Conn);
              Cmd.CommandType = CommandType.StoredProcedure;
              SqlDataAdapter da = new SqlDataAdapter(Cmd);
              DataTable dt = new DataTable("News");
              da.Fill(dt);
              CloseConnect();
              return dt;
          }
          catch (Exception ex)
          {
              throw ex;
          }

      }

      public int InsertNews(DTONews objnew)
      {
          int Result = 0;
          try
          {
              OpenConnect();
              Cmd = new SqlCommand("insertNews", Conn);
              Cmd.CommandType = CommandType.StoredProcedure;
              Cmd.Parameters.Add("@title", SqlDbType.VarChar, 100).Value = objnew.Title;
              Cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = objnew.Description1;
              
              
              Result = Cmd.ExecuteNonQuery();
              CloseConnect();
          }
          catch (Exception ex)
          {

              throw ex;
          }
          return Result;
      }

      public int Deletenews(DTONews objnew)
      {
          int Result = 0;
          try
          {
              OpenConnect();
              Cmd = new SqlCommand("deleteNews", Conn);
              Cmd.CommandType = CommandType.StoredProcedure;
              Cmd.Parameters.Add("@id", SqlDbType.Int).Value = objnew.Id;
              Result = Cmd.ExecuteNonQuery();
              CloseConnect();
          }
          catch (Exception ex)
          {

              throw ex;
          }
          return Result;

      }
      public int updateNews(DTONews objnew)
      {
          int Result = 0;
          try
          {
              OpenConnect();
              Cmd = new SqlCommand("UpdateNews", Conn);
              Cmd.CommandType = CommandType.StoredProcedure;
              Cmd.Parameters.Add("@id", SqlDbType.Int).Value = objnew.Id;
              Cmd.Parameters.Add("@title", SqlDbType.VarChar, 100).Value = objnew.Title;
              Cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = objnew.Description1;
              
              Result = Cmd.ExecuteNonQuery();
              CloseConnect();
          }
          catch (Exception ex)
          {

              throw ex;
          }
          return Result;

      }

      public DataTable GetnewByTitle(DTONews objnew)
      {
          try
          {
              Cmd = new SqlCommand("findNews", Conn);
              Cmd.CommandType = CommandType.StoredProcedure;
              Cmd.Parameters.Add("@title", SqlDbType.VarChar, 50).Value = objnew.Title;
              SqlDataAdapter da = new SqlDataAdapter(Cmd);
              DataTable dt = new DataTable("News");
              da.Fill(dt);
              return dt;
          }
          catch (Exception ex)
          {
              throw ex;
          }

      }
      public DataTable GetnewById(DTONews objnew)
      {
          try
          {
              Cmd = new SqlCommand("GetnewsById", Conn);
              Cmd.CommandType = CommandType.StoredProcedure;
              Cmd.Parameters.Add("@id", SqlDbType.Int).Value = objnew.Id;
              SqlDataAdapter da = new SqlDataAdapter(Cmd);
              DataTable dt = new DataTable("News");
              da.Fill(dt);
              return dt;
          }
          catch (Exception ex)
          {
              throw ex;
          }

      }
      public DataTable GetnewByTitle(string title)
      {
          try
          {
              Cmd = new SqlCommand("findNews", Conn);
              Cmd.CommandType = CommandType.StoredProcedure;
              Cmd.Parameters.Add("@title", SqlDbType.VarChar, 100).Value = title;
              SqlDataAdapter da = new SqlDataAdapter(Cmd);
              DataTable dt = new DataTable("News");
              da.Fill(dt);
              return dt;
          }
          catch (Exception ex)
          {
              throw ex;
          }

      }
    }
}
